package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.mapper.RoleMapper;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public List<Role> findAll() {
        return roleMapper.findAll();

    }

    /**
     * 新增
     * @author Qifei
     * @param role
     * @param menuIds
     * @param permissionIds
     */
    @Override
    public void add(Role role, Integer[] menuIds, Integer[] permissionIds) {
        roleMapper.add(role);
        if(null!=menuIds && menuIds.length!=0) {
            setRoleAndMenus(role.getId(), menuIds);
        }
        if(null!=permissionIds && permissionIds.length!=0) {
            roleMapper.setRoleAndPermissions(role.getId(), permissionIds);
        }
    }

    /**
     * 修改
     * @author Qifei
     * @param role
     * @param menuIds
     * @param permissionIds
     */
    @Override
    public void edit(Role role, Integer[] menuIds, Integer[] permissionIds) {
        roleMapper.edit(role);
        Integer roleId = role.getId();
        roleMapper.deleteRoleAndMenus(roleId);
        roleMapper.deleteRoleAndPermissions(roleId);
        if(null!=menuIds && menuIds.length!=0) {
            setRoleAndMenus(roleId, menuIds);
        }
        if(null!=permissionIds && permissionIds.length!=0) {
            roleMapper.setRoleAndPermissions(role.getId(), permissionIds);
        }
    }

    /**
     * 分页查询
     * @author Qifei
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleMapper.findPage(queryString);
        return new PageResult(page.getTotal(), page.getResult());

    }

    /**
     * 根据菜单id获取父菜单id，同时加入数据库
     * @author Qifei
     * @param roleId
     * @param menuIds
     */
    public void setRoleAndMenus(Integer roleId, Integer[] menuIds){
        List<Integer> parentIds = roleMapper.findParentIdsByIds(menuIds);
        Set<Integer> set = new HashSet<>(parentIds);
        for (Integer menuId : menuIds) {
            set.add(menuId);
        }
        roleMapper.setRoleAndMenus(roleId, set);
    }

    /**
     * 弹出编辑窗口
     * @author Qifei
     * @param roleId
     * @return
     */
    @Override
    public Map findRoleAndReShowByRoleId(Integer roleId) {
        List<Integer> menuIds = roleMapper.getMenuIdsByRoleId(roleId);
        List<Integer> permissionIds = roleMapper.getPermissionIdsByRoleId(roleId);
        Role role = roleMapper.findByRoleId(roleId);
        Map map = new HashMap();
        map.put("menuIds", menuIds);
        map.put("permissionIds", permissionIds);
        map.put("role", role);
        return map;
    }

    /**
     * 删除
     * @author Qifei
     * @param roleId
     */
    @Override
    public void delete(Integer roleId) {
        if(roleMapper.findUserCountByRoleId(roleId)>0){
            throw new RuntimeException("该角色还有用户关联");
        }
        roleMapper.deleteRoleAndMenus(roleId);
        roleMapper.deleteRoleAndPermissions(roleId);
        roleMapper.delete(roleId);
    }

    /**
     * 根据角色名称或者关键词模糊查询
     * @author Qifei
     * @param role
     * @return
     */
    @Override
    public Role findByNameOrKeyword(Role role) {
        return roleMapper.findByNameOrKeyword(role);
    }
}
